package com.roger.query.service.impl;

import org.apache.lucene.index.Term;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.spans.SpanNearQuery;
import org.apache.lucene.search.spans.SpanTermQuery;

import com.roger.query.service.QueryService;
import com.roger.vo.PhrasesQueryParams;

/**
 * 短语查询(表示要包含跨度的对象,短语的前后顺序不可变)
 * @author luoweiyi
 *
 */
public class SpanQueryServiceImpl implements QueryService<PhrasesQueryParams>{

	@Override
	public Query getQuery(PhrasesQueryParams params) {
		SpanTermQuery[] querys = new SpanTermQuery[params.getPhrases().length];
		for(int i = 0; i < querys.length; i++){
			querys[i] = new SpanTermQuery(new Term(params.getField(),params.getPhrases()[i]));
		}
		SpanNearQuery query = new SpanNearQuery(querys,params.getSlop(),true);
		return query;
	}

}
